#include "spwm_int.h"
#include "hdiv.h"
#include "spwm_timer0.h"

/*
    调制比本为小数,这里做整数处理,功率的调节对象是调制比
    sin正弦函数向上平移与定时器的PWM脉宽成比例->整数下
    
    定时器最大计数为9600，实为频率确定TimerArr_50Hz = (PLUSE_COUNT_50Hz / (AC_FREQ / 100));
*/

unsigned short M = 3000; //(PLUSE_COUNT_50Hz / (AC_FREQ / 100))/2;            //调制比=调制波峰值/载波峰值,定时器最大比较值9600,当M=1时,取9600.这里取为调制波峰值
//调制最大值不是限定为9600，而是由当前输出频率确定
unsigned short N = 0;             //载波比=载波频率/调制波频率,即50Hz内承载200个载波，即50*200=10kHz
unsigned short SPWMCnt = 128;         //由0递增到调制比
unsigned char SinCnt = 0;   
unsigned short  AC_FREQ = 50;//sin正弦数组计数由0递增到128 =(SPWMCnt/N)*128=SPWMCnt*128/N
unsigned short AC_FREQ_CAL;// = (AC_FREQ*100);
unsigned short SPWM_Freq;// = AC_FREQ*100; //SPWM调制波频率,*100倍
//unsigned short SPWM_Freq ;
unsigned short AC_DetectFreq = 0;   //探测到的交流频率
unsigned short TimerArr_50Hz; //= (PLUSE_COUNT_50Hz / (AC_FREQ_CAL));





//unsigned short TimerArr_50Hz = (PLUSE_COUNT_50Hz / freq);
//初始值9600,实际电网频率对应定时器的溢出值(不严格为50Hz)

SPWM_VAR spwm_var;

//sin正弦数组,1/4个周期
//用全周期,存储在flash中,上移1
//1/4周期的数
const unsigned short sineLite_Int[SINE_COUNT_LITE] = {
    0,
    3215,
    6423,
    9616,
    12785,
    15923,
    19024,
    22078,
    25079,
    28020,
    30893,
    33692,
    36409,
    39039,
    41575,
    44011,
    46340,
    48558,
    50659,
    52639,
    54491,
    56212,
    57797,
    59243,
    60547,
    61705,
    62714,
    63571,
    64276,
    64826,
    65220,
    65457,
    65535,
};

//sin*2^20=sin*1048576

unsigned short sine_Int[SINE_COUNT] = {0};//SINE_COUNT=128
unsigned long temp;
stc_div_unsigned_result_t result_div = {0, 0};
//sine值更新在主函数中,不要在中断执行
void sineValue_update(void)
{
    unsigned char i;
    unsigned char pos_start, pos_end, pos_sym;
    //unsigned long temp;
    unsigned short TimerArr_50Hz_div2;
	
//*****
	  AC_FREQ_CAL = (AC_FREQ*100);
	  SPWM_Freq = AC_FREQ*100;
    TimerArr_50Hz = (PLUSE_COUNT_50Hz / (AC_FREQ_CAL));
//******
	
    //SPWM_Freq 50Hz的交流电频率
    Hdiv_Unsigned(PLUSE_COUNT_50Hz*100, SPWM_Freq, &result_div);  //SPWM_Freq在定时时扩大了100倍,被除数同样扩大100倍
    TimerArr_50Hz = result_div.Quotient;
    App_Timer0SetARR(TimerArr_50Hz);   //调整基波定时器频率，以调整载波频率
    TimerArr_50Hz_div2 = TimerArr_50Hz>>1;  //后面不用重复计算, 0-最大值的中间值,比例以中间值缩减
    //   f/(N*fac)50Hz对应的定时器溢出值
    for (i = 0; i < SINE_COUNT_LITE; i++)
    {
        temp = sineLite_Int[i] * M;
        temp >>= 17;  //sin扩大16倍+/2=17, 正负部分故除以2
        temp += TimerArr_50Hz_div2;  //这里的除法不可少
        sine_Int[i] = (unsigned short)temp;
        //  sine[k]*M(小数)/2*CarrVal+1/2*CarrVal=sine[k]*M(整数)/2+1/2*CarrVal
    }
    pos_end = SINE_COUNT_LITE+32; //32个对称
    pos_sym = 32*2;//关于[32]对称
    for(i = SINE_COUNT_LITE; i<pos_end; i++)  //关于[32]对称
    {
        sine_Int[i] = sine_Int[pos_sym-i];
    }
    /****以上为变化为正的部分*******/

    //按中心对称计算    
    //Y值关于X=TimerArr_50Hz_div2对称，关于点(64,TimerArr_50Hz_div2)中心对称
    pos_start = pos_end;  //64
    pos_end = pos_start + 64;
    pos_sym = 64*2;//关于[64]对称
    for(i = pos_start; i<pos_end; i++)  //关于[32]对称
    {
        sine_Int[i] = TimerArr_50Hz - sine_Int[pos_sym-i];
    } 
}

void sinFreq_update(void)
{
    stc_div_unsigned_result_t result_div = {0, 0};
    
}